package com.wanlian.router.ui.main.yun;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.smart.android.ui.tools.BaseRecyclerListFragment;
import com.wanlian.router.R;
import com.wanlian.router.bean.FileType;
import com.wanlian.router.bean.YunPictureGroup;
import com.wanlian.router.event.RefreshEvent;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hyu on 2018/9/3.
 * Email: fvaryu@163.com
 */
public class YunPictureFragment extends BaseRecyclerListFragment {

    private List<YunPictureGroup> mYunPictureGroups;
    private YunPictureAdapter mYunPictureAdapter;

    private RecyclerView mRecyclerView;
    public static YunPictureFragment newInstance() {
        return new YunPictureFragment();
    }


    @Override
    protected int layout() {
        return R.layout.fragment_pull_recyclerview;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        mRecyclerView = view.findViewById(R.id.recycler_view);
        mYunPictureGroups = new ArrayList<>();

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        mRecyclerView.setAdapter(mYunPictureAdapter = new YunPictureAdapter(mYunPictureGroups));
    }

    @Override
    protected void initData(View view) {
        super.initData(view);


        getEmptyView().setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image_empty_file, 0, 0);
        getEmptyView().setText("还没有上传照片哦~");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void reload(RefreshEvent event) {
        if (event.getFileType().ordinal() == FileType.Picture.ordinal()) {
            reload();
        }
    }

    public void reload() {
        autoRefresh();
    }

    @Override
    protected void loadData(final boolean isRefresh) {
        super.loadData(isRefresh);

        if (!canToLoad(isRefresh)) {
            return;
        }

        NetWorkingClient.getInstance().queryYunPictureFiles(getActivity(), getPageInfo(), FileType.Picture.ordinal(), new INetCallBack<List<YunPictureGroup>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<YunPictureGroup> yunPictureGroups) {
                resetPull();
                if (response.isSuccess()) {
                    if (isRefresh) {
                        mYunPictureGroups.clear();
                    }
                    if (yunPictureGroups != null) {
                        mYunPictureGroups.addAll(yunPictureGroups);
                    }
                    mYunPictureAdapter.notifyDataSetChanged();
                }

                if (mYunPictureGroups.isEmpty()) {
                    mRecyclerView.setVisibility(View.GONE);
                    showEmpty();
                } else {
                    mRecyclerView.setVisibility(View.VISIBLE);

                    hiddenEmpty();
                }

            }
        });
    }
}
